﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using kTudu.Web.Controllers.Base;
using kTudu.Web.Models;
using kTudu.Web.Applications;
using kTudu.Web.Factories;

namespace kTudu.Web.Controllers
{
    public class AccountController : BaseController
    {
        protected UserClientApplication _application = null;
        protected RolApplication _rolApplication = null;

        public AccountController()
        {
            _application = _applicationFactory.GetApplication<UserClientApplication>();
            _rolApplication = _applicationFactory.GetApplication<RolApplication>();
        }
        
        //
        // GET: /Account/
        public ActionResult Index()
        {
            return TryViewResult(() =>
            {
                if (CurrentUser.IdUserClient > 0)
                {
                    return View(CurrentUser);
                }
                else
                {
                    // Error, no podria entrar aca si no esta logueado
                }

                return View();
            });
        }

        [HttpPost]
        public ActionResult Edit(UserClient model)
        {
            return TryActionResult(() =>
            {
                if (ModelState.IsValid)
                {
                    _application.Update(model);
                }
                
                return View(model);
            });
        }

        public ActionResult Edit(int id)
        {
            return TryActionResult(() =>
            {
                UserClient userClient = _application.Get(id);

                SetViewBag(userClient);

                return View(userClient);
            });
        }

        protected void SetViewBag(UserClient userClient)
        {
            ViewBag.IdRol = new SelectList(_rolApplication.GetAll(), "IdRol", "Description", userClient.IdRol);
            ViewBag.RolDescription = "Administrador";
        }
    }
}
